package day07;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author zouran
 * createDate：2024/6/28  16:53
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(10000);
        System.out.println("Server Start！");
        Socket socket = server.accept();
        char[] chars; //发送的最大消息不超过1024*1024 个字符
        String message = "Nice to meet you too!";
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        while (true) {
            chars = new char[1024];
            int len = bufferedReader.read(chars);
            System.out.print("Receive Client Message: ");
            System.out.println(new String(chars, 0, len));
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please input message:");
            message = scanner.nextLine();
            bufferedWriter.write(message);
            bufferedWriter.flush();
            System.out.println("Send Success!");
        }
    }
}
